package com.rongshu.view.userInfo.activity;

import android.content.Intent;
import android.support.v7.widget.DefaultItemAnimator;
import android.support.v7.widget.LinearLayoutManager;
import android.view.View;

import com.rongshu.R;
import com.rongshu.utils.Common;
import com.rongshu.utils.Constant;
import com.rongshu.utils.UserInfoUtils;
import com.rongshu.view.userInfo.adapter.DirectMessageAdapter;
import com.rongshu.view.userInfo.entity.DirectMessage;
import com.rongshu.view.userInfo.iview.DirectMessageView;
import com.rongshu.view.userInfo.presenter.DirectMessagePresenter;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class DirectMessageListActivity extends MessageListActivity<DirectMessagePresenter> implements DirectMessageView {
    //adapter
    private DirectMessageAdapter adapter = null;
    //map
    private Map<String, Object> map = null;
    //request
    private final int ENTER_CHAT_REQUEST = 0x01;
    //position
    private int position = 0;
    //私信列表
    private List<DirectMessage.ResponseBean> directMessageList = null;

    @Override
    protected void initView() {
        super.initView();
        headMiddle.setText("私信");
        //set presenter
        setPresenter(new DirectMessagePresenter(this), this);
        //layoutmanager
        LinearLayoutManager manager = new LinearLayoutManager(this);
        recyclerView.setLayoutManager(manager);
        //动画
        DefaultItemAnimator defaultItemAnimator = new DefaultItemAnimator();
        defaultItemAnimator.setRemoveDuration(100);
        recyclerView.setItemAnimator(defaultItemAnimator);
        //adapter
        directMessageList = new ArrayList<>();
        adapter = new DirectMessageAdapter(this, directMessageList);
        adapter.setHasStableIds(true);
        adapter.setOnItemClickListener(this);
        recyclerView.setAdapter(adapter);
    }

    @Override
    protected void requestMessage() {
        //user_id
        int user_id = UserInfoUtils.getUserId();
        map = new HashMap<>();
        map.put("user_id", user_id);
        map.put("pageIndex", pageIndex);
        map.put("pageSize", pageSize);
        presenter.getMyPrivateMessage(user_id,
                pageIndex, pageSize, Common.encryptionSign(map));
        map.clear();
    }

    @Override
    protected void setAllRead() {
        presenter.GetMsgAllRead();
        for (int index = 0; index < directMessageList.size(); index++) {
            directMessageList.get(index).setCount(0);
        }
        adapter.notifyDataSetChanged();
    }

    @Override
    public void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (resultCode == RESULT_OK) {
            if (requestCode == ENTER_CHAT_REQUEST) {
                if (data == null) {
                    return;
                }
                messageNum = messageNum - directMessageList.get(position).getCount();
                if (messageNum <= 0) {
                    txt_right.setVisibility(View.GONE);
                }
                String contents = data.getStringExtra(Constant.INTENT_DATA);
                String create_datetime = data.getStringExtra(Constant.INTENT_MESSAGE);
                directMessageList.get(position).getPrivatechatList().setContents(contents);
                directMessageList.get(position).setCount(0);
                directMessageList.get(position).getPrivatechatList().setCreate_datetime(create_datetime);
                adapter.notifyItemChanged(position);
            }
        }
    }


    /**
     * 删除私信
     */
    private void getDeletePrivatechat(int receive_userid) {
        //user_id
        int user_id = UserInfoUtils.getUserId();
        map = new HashMap<>();
        map.put("send_userid", user_id);
        map.put("receive_userid", receive_userid);
        presenter.getDeletePrivatechat(
                (int) map.get("send_userid"),
                (int) map.get("receive_userid"),
                Common.encryptionSign(map));
        map.clear();
    }

    @Override
    public void onItemClick(int position) {
        this.position = position;
        Intent intent = new Intent(this, DirectChatActivity.class);
        intent.putExtra(Constant.INTENT_DATA, directMessageList.get(position));
        startActivityForResult(intent, ENTER_CHAT_REQUEST);
    }

    @Override
    public void onItemDeleteClick(int position) {
        int receive_userid = directMessageList.get(position).getNewSend_userid();
        getDeletePrivatechat(receive_userid);
        directMessageList.remove(position);
        adapter.notifyItemRemoved(position);//注意这里
        adapter.notifyItemRangeChanged(position, directMessageList.size() - position);//通知数据与界面重新绑定
    }

    @Override
    public void onUserHeaderClick(int userIdTo) {

    }

    /**
     * 我的私信
     */
    @Override
    public void onDirectMessageList(DirectMessage directMessage) {
        if (directMessage != null) {
            List<DirectMessage.ResponseBean> authorDetailBeans = directMessage.getResponse();
            if (refreshLayout.isRefreshing()) {//结束刷新
                refreshLayout.finishRefresh();
            }
            if (refreshLayout.isLoading()) {//结束加载
                refreshLayout.finishLoadmore();
            }
            if (pageIndex == 1) {
                directMessageList.clear();
            }
            directMessageList.addAll(authorDetailBeans);
            adapter.notifyDataSetChanged();
            if (authorDetailBeans.size() == 0) {//请求不到数据，证明没有更多了
                refreshLayout.finishLoadmoreWithNoMoreData();
            }
            if (directMessageList.size() == 0) {//判断显示缺省页面
                showEmptyLayout();
            } else {
                hideEmptyLayout();
            }
        }
    }
}
